package com.x.example;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.x.uikit.service.IPageRouteService;
import com.x.example.demo.ArticleToolbarDemo;
import com.x.example.demo.BottomTabDemo;
import com.x.example.demo.MainPage;
import com.x.example.demo.TestPage;
import com.x.example.demo.WebPage;
import com.x.uikit.page.XPage;
import com.x.uikit.page.XPageManager;
import com.x.uikit.router.XRoute;
import com.x.uikit.service.XService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PageRouteList {
    public static String NATIVE_TEST = "native://test";
    public static String NATIVE_WEB = "native://web";
    public static String NATIVE_MAIN = "native://main";
    public static String NATIVE_ARTICLE_TOOLBAR = "native://article_toolbar";
    public static String NATIVE_BOTTOM_TAB = "native://bottom_tab";
    public static String NATIVE_MULTI_CHANNEL = "native://native_multi_channel";
    public static String[] ROUTES = new String[]{
            NATIVE_TEST,
            NATIVE_WEB,
            NATIVE_MAIN,
            NATIVE_ARTICLE_TOOLBAR,
            NATIVE_BOTTOM_TAB,
    };
    public static Map<String, IPageRouteService.PageLoader> sNavigatorMap = new ConcurrentHashMap<>();

    static {
        sNavigatorMap.put(NATIVE_MAIN, new IPageRouteService.PageLoader() {
            @Override
            public XPage load(@NonNull XRoute route, @NonNull Context context) {
                MainPage page = new MainPage(context);
                page.setArgument(route.argument);
                return page;
            }
        });
        sNavigatorMap.put(NATIVE_WEB, new IPageRouteService.PageLoader() {
            @Override
            public XPage load(@NonNull XRoute route, @NonNull Context context) {
                WebPage page = new WebPage(context);
                page.setArgument(route.argument);
                return page;
            }
        });
        sNavigatorMap.put(NATIVE_TEST, new IPageRouteService.PageLoader() {
            @Override
            public XPage load(@NonNull XRoute route, @NonNull Context context) {
                TestPage page = new TestPage(context);
                page.setArgument(route.argument);
                return page;
            }
        });
        sNavigatorMap.put(NATIVE_ARTICLE_TOOLBAR, new IPageRouteService.PageLoader() {
            @Override
            public XPage load(@NonNull XRoute route, @NonNull Context context) {
                ArticleToolbarDemo page = new ArticleToolbarDemo(context);
                page.setArgument(route.argument);
                return page;
            }

        });
        sNavigatorMap.put(NATIVE_BOTTOM_TAB, new IPageRouteService.PageLoader() {
            @Override
            public XPage load(@NonNull XRoute route, @NonNull Context context) {
                BottomTabDemo page = new BottomTabDemo(context);
                page.setArgument(route.argument);
                return page;
            }
        });
    }

    public static String findPath(XRoute route) {
        if (route != null) {
            for (String path : PageRouteList.ROUTES) {
                if (route.path.startsWith(path)) {
                    return path;
                }
            }
        }
        return null;
    }

    public static void navigate(@NonNull XRoute route, @NonNull XPageManager manager) {
        String path = findPath(route);
        if (TextUtils.isEmpty(path)) {
            return;
        }
        IPageRouteService.PageLoader handler = sNavigatorMap.get(path);
        IPageRouteService service = XService.get(IPageRouteService.class);
        if (service != null && handler != null) {
            service.navigate(route, handler);
        }
    }

    public static boolean hasPath(XRoute route) {
        return findPath(route) != null;
    }

}
